package name.cyrax.red.jpv.impl.funcs.core;

import name.cyrax.red.jpv.api.adapters.NumericAdapter;
import name.cyrax.red.jpv.api.funcs.Functor;
import name.cyrax.red.jpv.api.funcs.FunctorInstance;
import name.cyrax.red.jpv.api.funcs.info.FunctorMethod;

public class AbstractFunctorInstance implements FunctorInstance
{
	private final Functor functor;
	private final FunctorMethod functorMethod;
	private final Object targetObject;
	
	public AbstractFunctorInstance(Functor functor, FunctorMethod functorMethod, Object targetObject) {
		this.functor = functor;
		this.functorMethod = functorMethod;
		Class<?> tc = functorMethod.target();
		this.targetObject = tc == null ? targetObject : tc.cast(targetObject);
	}

	@Override public Functor getFunctor() {
		return functor;
	}	
	@Override public FunctorMethod getMethodInfo() {
		return functorMethod;
	}
	@Override public Object getTargetObject(NumericAdapter caller) {
		return targetObject == null ? caller : targetObject;
	}	
}
